package com.ibm.spatiotemp.test.query;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.ibm.spatiotemp.datamodel.config.BackendServerManager;
import com.ibm.spatiotemp.datamodel.config.CacheServerManager;
import com.ibm.spatiotemp.datamodel.datatype.TemporalLineString;
import com.ibm.spatiotemp.datamodel.result.SpatioObjectResultSet;
import com.ibm.spatiotemp.processor.RtreeSearcher2;

import edu.umn.cs.spatialHadoop.core.Point;
import edu.umn.cs.spatialHadoop.core.Rectangle;

public class RtreeSearcherTest3 {
	
	public static Logger logger = Logger.getLogger(RtreeSearcherTest3.class);
	
	String r_tree_path;
	RtreeSearcher2<TemporalLineString> r_searcher_mem;
	RtreeSearcher2<TemporalLineString> r_searcher_disk;
	Rectangle query_rec;
	Point query_point;
	
	@Before
	public void setUp() throws Exception {
		CacheServerManager.init("conf/redisservers.conf");
		BackendServerManager.init("conf/backendinit.conf");
		
		//linestring R-tree index path
		r_tree_path	 = "test_index/data_00001.rtree";
		query_rec = new Rectangle(380,430,400,450);
		query_point = new Point(0,500);
		 
		if(r_searcher_mem == null)
			r_searcher_mem = new RtreeSearcher2<TemporalLineString>(r_tree_path, 
					new TemporalLineString(),true);
		
		if(r_searcher_disk == null)
			r_searcher_disk = new RtreeSearcher2<TemporalLineString>(r_tree_path, 
					new TemporalLineString(),false);
	}
	
	@Test
	public void Range_Query_MemBased()
	{
		try {
			int cnt = 0;
			SpatioObjectResultSet rset = r_searcher_mem.Range_Query(query_rec);
			
			while(rset.next())
			{
				Object obj = rset.getObject(0);
				System.out.println(obj);
				++cnt;
			}
			
			rset.close();
			System.out.println("total result size: " + cnt);
		} catch (Exception e) {
			logger.error(e);
		}
		
	}

	
	@Test
	public void KNN_Query_MemBased()
	{
		try {
			int cnt = 0;
			SpatioObjectResultSet rset = r_searcher_mem.KNN_Query(query_point,1);
			while(rset.next())
			{
				Object obj = rset.getObject(0);
				System.out.println(obj);
				++cnt;
			}
			
			rset.close();
			System.out.println("total result size: " + cnt);
		} catch (Exception e) {
			logger.error(e);
		}
		
	}

	@Test
	public void Range_Query_DiskBased()
	{
		try {
			int cnt = 0;
			SpatioObjectResultSet rset = r_searcher_disk.Range_Query(query_rec);
			
			while(rset.next())
			{
				Object obj = rset.getObject(0);
				System.out.println(obj);
				++cnt;
			}
			
			rset.close();
			System.out.println("total result size: " + cnt);
		} catch (Exception e) {
			logger.error(e);
		}
		
	}

	
	@Test
	public void KNN_Query_DiskBased()
	{
		try {
			int cnt = 0;
			SpatioObjectResultSet rset = r_searcher_disk.KNN_Query(query_point,1);
			while(rset.next())
			{
				Object obj = rset.getObject(0);
				System.out.println(obj);
				++cnt;
			}
			
			rset.close();
			System.out.println("total result size: " + cnt);
		} catch (Exception e) {
			logger.error(e);
		}
		
	}
	
	@After
	public void ClearUp(){
		r_searcher_mem.close();
		r_searcher_disk.close();
	}

}
